package yxy.game.pm2.command.soul;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.souldata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;

/** 魂玉 魂玉分解 */
@MessageType(proto = Command.SoulResolve, classtype = HeroMessage.SoulResolveRequest.class)
public class CMD_SoulResolve extends Command<HeroMessage.SoulResolveRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.SoulResolveRequest request) {
		String playerCode = messageSource.getTag();
		String soulCode = request.getCode();

		HeroMessage.SoulResolveResponse.Builder response = HeroMessage.SoulResolveResponse.newBuilder();

		Soul soul = Soul.get(playerCode, soulCode);
		if (soul == null || soul.getHero() != null) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		souldata data = DataCenter.getData(soul.getDataId(), souldata.class);
		Currency back = Currency.create(data.getResolvetype(), data.getResolveid(), data.getResolvenum());
		new CurrencyHandle().addCurrency(playerCode, CCReason.魂玉, back);

		Soul.remove(playerCode, soul);

		response.setCode(soul.getCode());
		response.addItem(new CurrencyHandle().packItem(back));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, HeroMessage.SoulResolveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}